<%+cbi/valueheader%>
<script type="text/javascript">
	var US915_CHAN_PLAN = [
	['0_7,64', "channel 0 ~ channel 7, channel 64"],
	['8_15,65', "channel 8 ~ channel 15, channel 65"],
	['16_23,66', "channel 16 ~ channel 23, channel 66"],
	['24_31,67', "channel 24 ~ channel 31, channel 67"],
	['32_39,68', "channel 32 ~ channel 39, channel 68"],
	['40_47,69', "channel 40 ~ channel 47, channel 69"],
	['48_55,70', "channel 48 ~ channel 55, channel 70"],
	['56_63,71', "channel 56 ~ channel 63, channel 71"],
	['0_15,64_65', "channel 0 ~ channel 15, channel 64 ~ channel 65 ( 16 channels )"],
	['16_31,66_67', "channel 16 ~ channel 31, channel 66 ~ channel 67 ( 16 channels )"],
	['32_47,68_69', "channel 32 ~ channel 47, channel 68 ~ channel 69 ( 16 channels )"],
	['48_63,70_71', "channel 48 ~ channel 63, channel 70 ~ channel 71 ( 16 channels )"],
	];
	var AU915_CHAN_PLAN = [
	['0_7,64', "channel 0 ~ channel 7, channel 64"],
	['8_15,65', "channel 8 ~ channel 15, channel 65"],
	['16_23,66', "channel 16 ~ channel 23, channel 66"],
	['24_31,67', "channel 24 ~ channel 31, channel 67"],
	['32_39,68', "channel 32 ~ channel 39, channel 68"],
	['40_47,69', "channel 40 ~ channel 47, channel 69"],
	['48_55,70', "channel 48 ~ channel 55, channel 70"],
	['56_63,71', "channel 56 ~ channel 63, channel 71"],
	['0_15,64_65', "channel 0 ~ channel 15, channel 64 ~ channel 65 ( 16 channels )"],
	['16_31,66_67', "channel 16 ~ channel 31, channel 66 ~ channel 67 ( 16 channels )"],
	['32_47,68_69', "channel 32 ~ channel 47, channel 68 ~ channel 69 ( 16 channels )"],
	['48_63,70_71', "channel 48 ~ channel 63, channel 70 ~ channel 71 ( 16 channels )"],
	];
	var CN470_CHAN_PLAN = [
	['0_7', "channel 0 ~ channel 7"],
	['8_15', "channel 8 ~ channel 15"],
	['16_23', "channel 16 ~ channel 23"],
	['24_31', "channel 24 ~ channel 31"],
	['32_39', "channel 32 ~ channel 39"],
	['40_47', "channel 40 ~ channel 47"],
	['48_55', "channel 48 ~ channel 55"],
	['56_63', "channel 56 ~ channel 63"],
	['64_71', "channel 64 ~ channel 71"],
	['72_79', "channel 72 ~ channel 79"],
	['80_87', "channel 80 ~ channel 87"],
	['88_95', "channel 88 ~ channel 95"],

	['0_15', "channel 0 ~ channel 15"],
	['16_31', "channel 16 ~ channel 31 ( 16 channels )"],
	['32_47', "channel 32 ~ channel 47 ( 16 channels )"],
	['48_63', "channel 48 ~ channel 63 ( 16 channels )"],
	['64_79', "channel 64 ~ channel 79 ( 16 channels )"],
	['80_95', "channel 80 ~ channel 95 ( 16 channels )"],
	];
	function applyFsb(fsbVal){
		var regObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region")
		var fsbObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.FSB")

		fsbObj.val(fsbVal);
		if( fsbVal == 'custom'){
			$("#cbi-lora_pkt_fwd-freq_plan-freq").show();
			$("#cbi-lora_pkt_fwd-freq_plan-lora_std").show();
			return;
		}

		$("#cbi-lora_pkt_fwd-freq_plan-freq").hide();
		$("#cbi-lora_pkt_fwd-freq_plan-lora_std").hide();

		var s_e = fsbVal.split(',')[0].split('_');
		var std_s_e = fsbVal.split(',')[1] ? fsbVal.split(',')[1].split('_') : undefined;
		console.log("std_s_e", std_s_e);
		var region = regObj.val();
		var multiSFList = [];
		var lorsStdList = [];

		for( var i = parseInt(s_e[0]); i <= parseInt(s_e[1]);i++){
			multiSFList.push({ freq : (eval(region + "_FREQ_MIN")*1000000 + 200000 * i)/1000000, type : 'multiSF'});
		}
		if( std_s_e ){
			for( var i = parseInt(std_s_e[0]); i <= parseInt(std_s_e[1]?std_s_e[1]:std_s_e[0]); i++ ){
				lorsStdList.push({ freq: (eval(region + "_STD_FREQ_MIN")*1000000 + eval(region + "_STD_FREQ_STEP")*1000000*(i-64))/1000000, sf : eval(region +"_STD_SF"), bw : eval(region +"_STD_BW")})
			}
		}
		init_freqlist(multiSFList);	
		init_lora_list(lorsStdList);
		init_fsk_list([]);
		caculate_radio_params();
	}
	function appendFSB(){
		var regObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region")
		var fsbObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.FSB")
		console.log(regObj.val())
		var fsbList = eval(regObj.val() + "_CHAN_PLAN");
		fsbObj.html("");
		fsbList.forEach(function(v, i){
			fsbObj.append("<option value='"+v[0]+"'>"+v[1]+"</option>")
		});
		fsbObj.append("<option value='custom'>Custom</option>");
	}
	function regChage(fsb){
		var regObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region")
		var fsbObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.FSB")
		switch(regObj.val()){
			case 'AU915':
			case 'CN470':
			case 'US915':
			$("#cbi-lora_pkt_fwd-freq_plan-FSB").show();			
			appendFSB();
			if( fsb ){
				applyFsb(fsb);
			}
			else{
				var fsbList = eval(regObj.val() + "_CHAN_PLAN");
				applyFsb(fsbList[0][0]);
			}
			break;
			default:
			$("#cbi-lora_pkt_fwd-freq_plan-FSB").hide();
			$("#cbi-lora_pkt_fwd-freq_plan-freq").show();
			$("#cbi-lora_pkt_fwd-freq_plan-lora_std").show();
			break;
		}
	}
	$(document).ready(function(){
		var regObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.region")
		var fsbObj = $("#cbid\\.lora_pkt_fwd\\.freq_plan\\.FSB")
		
		
		fsbObj.change(function(){
			applyFsb(fsbObj.val())
		});
		
		
		
		regChage("<%=tostring(self:cfgvalue(section) or self.default)%>");
		//applyFsb("<%=tostring(self:cfgvalue(section) or self.default)%>")
	});
</script>
<select class="cbi-input-select" onchange="cbi_d_update(this.id)"<%= attr("id", cbid) .. attr("name", cbid) .. ifattr(self.size, "size") %>>

</select>
<%+cbi/valuefooter%>